package com.gitee.huanminabc.utils_tools.log_extend.feign;
import com.gitee.huanminabc.jcommon.base.SnowIdUtil;
import com.gitee.huanminabc.jcommon.str.StringUtil;
import com.gitee.huanminabc.utils_tools.log_extend.Constants;
import com.gitee.huanminabc.utils_tools.log_extend.LogTraceContextHolder;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;


/**
 * @author 胡安民
 *
 * 支持feign调用时，trace放入header
 */
@Configuration
@Order(1)
public class FeignLogTraceInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        String traceId = LogTraceContextHolder.get();
        if (StringUtil.isEmpty(traceId)){
            traceId = SnowIdUtil.uniqueLongHex() ;
        }
        requestTemplate.header(Constants.LOG_TRACE_ID_HEADER,traceId);
    }
}
